package com.imc.platform.base.login.controller;

import com.alibaba.fastjson.JSONObject;
import com.imc.core.utils.MysqlUtil;
import com.imc.platform.base.home.service.IHomeService;
import com.imc.platform.base.notice.pojo.Notice;
import com.imc.platform.base.user.pojo.User;
import com.twp.common.tuple.ResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;
import java.util.List;
import java.util.Map;

/**
 * Created by tanweiping on 2018/8/2.
 */
/*
* 类描述：主页控制器
* 开发者：tanweiping
* 创建日期：2018/8/7 10:31
*/
@Controller
public class HomeController {

    @Autowired
    IHomeService iHomeService;
    @Autowired
    MysqlUtil mysqlUtil;

    /*
    * tanweiping  2018/8/7 10:31
    * 方法描述：主页页面
    */
    @GetMapping("/home/index")
    public String homePage(){
        return "frame/index";
    }

    /**
     * 方法描述:公告查询
     * SJC 2018-8-22 10:24:41
     */
    @RequestMapping("/home/queryNoticeByHome")
    @ResponseBody
    public ResultDTO queryNoticeByHome(Principal principal) throws Exception {
        String uname = principal.getName();
        User user = mysqlUtil.getUserByLoginName(uname);
        JSONObject map = new JSONObject();
        //我发送的消息
        List<Map<String,Object>> isendList = iHomeService.querySendMsg(String.valueOf(user.getId()));
        map.put("isendList", isendList);
        //我接收的消息
        List<Map<String,Object>> ireceivedList = iHomeService.queryReceivedMsg(String.valueOf(user.getId()));
        map.put("ireceivedList", ireceivedList);
        //我发送的数量
        String sendCount= iHomeService.querySendMsgCount(String.valueOf(user.getId()));
        map.put("sendCount",sendCount);
        //我接收的消息
        String receivedCount = iHomeService.queryReceivedMsgCount(String.valueOf(user.getId()));
        map.put("receivedCount",receivedCount);

        ResultDTO obj = ResultDTO.builder().data(map).build();
        return obj;
    }

    @RequestMapping(value = "/home/queryNewsByHome",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO queryNewsByHome(){
        ResultDTO obj = ResultDTO.builder().data(iHomeService.queryNewsByHome()).build();
        return obj;
    }

    @RequestMapping(value = "/home/queryLessonByHome",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO queryLessonByHome(){
        ResultDTO obj = ResultDTO.builder().data(iHomeService.queryLessonByHome()).build();
        return obj;
    }

    @RequestMapping(value = "/home/queryNoticeByHomeByTitle",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO queryNoticeByHomeByTitle(String title){
        return ResultDTO.builder().data(iHomeService.queryNoticeByHomeByTitle(title)).build();
    }
}
